#!/usr/local/bin/perl;

$file = $ARGV[0];
$mode = $ARGV[1] || "events";
$SCREEN = $ARGV[2] || 1000000;

open(INPUT, $file) || die("Cannot open reading file.");
@raw_data = <INPUT>;
close(INPUT);

print "\n\nGetting Data...\n";

@terms = ();
$new = 0;
$newterm = "";
for ($i = 0; $i < $#raw_data+1; $i++) {
    $line = $raw_data[$i];
    if($line =~ m/{trace_ts/) {
	if(($line =~ m/[0-9]*}}\n/) || ($line =~ m/[0-9]*}}\.\n/)) {
		$newterm = "".$line;
		push(@terms, $newterm);
		$new = 0;
		$newterm = "";
	}
else {
	$new = 1;
	$newterm = $newterm.$line;
}

}
elsif($new = 1) {
	$newterm = $newterm.$line;
if (($line =~ m/[0-9]*}}\n/) || ($line =~ m/[0-9]*}}\.\n/)) {
	$new = 0;
	push(@terms, $newterm);
	$newterm = "";
}
}
else {}
}

@events = ();
@spawns = ();

$send_n = 0;
$spawn_n = 0;
$rec_n = 0;
$die_n = 0;

%hash = ();

for ($i = 0; $i < $#terms+1; $i++) {
    $term = $terms[$i];
$term =~ m/({[0-9]*,[0-9]*,[0-9]*}}[\.\n])/;
$tmp_ts = substr($1, 1, length($1)-4);
@term_ar = split(/,/, $term);
@time = split(/,/, $tmp_ts);
$ts = "";
#print "\nOrig : $tmp_ts";
for ($j = 0; $j < 3; $j++) {
	while (length($time[$j]) < 6) {
	    $time[$j] = "0".$time[$j];
	}
	$ts = $ts.$time[$j];
}
#print "     New : $ts\n"; 
if ($i == 0) {
	$first_ts = $ts;
}
$event_ts = $ts - $first_ts;
#print "Equation: ".$ts." - ".$first_ts." = ".$event_ts."\n";
$pid = $term_ar[1];
$type = $term_ar[2];
$spawn = "none";
$rec = "none";
$msg = "";
$event = "NO_EVENT";
$incr = int($event_ts / $SCREEN);
$ss = $hash{ $incr };
if ($ss =~ m/[0-9]*\.[0-9]*/) {
}
else {
	$hash{ $incr } = "0.0";
	$ss = "0.0";
}
@send_spawn = split(/\./, $ss);
if ($type eq "send") {
	$msg = $term_ar[3];
	$rec = $term_ar[$#term_ar-3];
	for ($j = 4; $j < $#term_ar-3; $j++) {
	    $msg = $msg.",".$term_ar[$j];
	}
	$rec =~ s/[^a-z#0-9\.<>]*//;
	$msg =~ tr/\n/\xAA/;
	$event = "--||send||$event_ts||$pid||$rec||$msg||--\n";
	$send_n++;
	$send_spawn[0] = $send_spawn[0] + 1;
	$ss = $send_spawn[0].".".$send_spawn[1];
	$hash{ $incr } = $ss;
}
elsif ($type eq "spawn") {
	$msg = $term_ar[4];
	$spawn = $term_ar[3];
	for($j = 5; $j < $#term_ar-2; $j++) {
	    $msg = $msg.",".$term_ar[$j];
	}
	$msg =~ tr/\n/\xAA/;
	$event = "--||spawn||$event_ts||$pid||$spawn||$msg||--\n";
	$spawn_n++;
	$send_spawn[1] = $send_spawn[1] + 1;
	$ss = $send_spawn[0].".".$send_spawn[1];
	$hash{ $incr } = $ss;
	if ($mode eq "spawn"){
		$spwn_ev = "$event_ts||$pid||$msg\n";
		push(@spawns, $spwn_ev);
	}
}
else {
	$msg = $term_ar[3];
	for($j = 4; $j < $#term_ar-2; $j++) {
	    $msg = $msg.",".$term_ar[$j];
	}
	$msg =~ tr/\n/\xAA/;
	if ($type eq "exit")  {
	    $event = "--||die||$event_ts||$pid||--\n";
	    $die_n++;
	}
	elsif ($type eq "'receive'") {
	    $event = "--||receive||$event_ts||$pid||$msg||--\n";
	    $rec_n++;
	}
}
if ($mode eq "events") {
	if ($event ne "NO_EVENT") {
	    push(@events, $event);
	}
}
}

if ($mode eq "events") {
    open(OUTPUT, ">$file.ev") || die("Cannot open writing file");
    print OUTPUT ($#events+1)."\n";
#    print OUTPUT ($send_n)."\n";
#    print OUTPUT ($spawn_n)."\n";
#    print OUTPUT ($rec_n)."\n";
#    print OUTPUT ($die_n)."\n";
    foreach $event (@events) {
		print OUTPUT $event;
    }
    close(OUTPUT);
    print "Done script.";
}
if ($mode eq "stats") {
    open(OUTPUT, ">$file.csv") || die("Cannot open writing file");
    print OUTPUT "time,sends,spawns\n";
    for $key ( sort keys %hash ) {
		$ss = $hash{ $key };
		@send_spawn = split(/\./, $ss);
		$stat = $key.",".$send_spawn[0].",".$send_spawn[1];
		print OUTPUT $stat."\n";
    }
    close(OUTPUT);
}
if ($mode eq "spawn") {
    open(OUTPUT, ">$file.txt") || die ("cannot open file");
    print OUTPUT "time, spawner, msg\n";
    foreach $spawn (@spawns) {
		print OUTPUT $spawn; 
    }
}
